
package controllers;

import play.data.*;
import play.mvc.*;
import models.*;
import views.html.confermaRegistrazione;
import views.html.home;
import views.html.categorie_ditte.nuova_categoria_ditta;
import views.html.ditte.*;

public class Ditte extends Controller {
	
	static Form<Ditta> dittaForm = Form.form(Ditta.class);

	//SHOW
	@Security.Authenticated(Secured.class)
	public static Result ditte() {
		return ok(ditta.render(Ditta.find.orderBy("nome").findList(), dittaForm,false));
	}
	
	//NEW
	@Security.Authenticated(SecuredWriter.class)
	public static Result newDitta() {
		Form<Ditta> filledForm = dittaForm.bindFromRequest();
		
		
		if(filledForm.hasErrors()) {
			return badRequest(nuova_ditta.render(filledForm,false));
		} 
		else{
					  
			if(!Ditta.create(filledForm.get())){
				return ok(nuova_ditta.render(filledForm,true));
			}
			return ok(ditta.render(Ditta.all(), dittaForm,true));
		}
	}
	
	//REGISTRA (NEW con Captcha)
	  
		public static Result registraDitta() {
			Form<Ditta> filledForm = dittaForm.bindFromRequest();
			
			
			if(filledForm.hasErrors()) {
				return badRequest(registra_ditta.render(filledForm,false));
			} 
			else{
						  
				if(!Ditta.create(filledForm.get())){
					return ok(registra_ditta.render(filledForm,true));
				}
				
				//prima invia la mail di conferma
				Mail.sendMail(filledForm.get());
				return ok(confermaRegistrazione.render());
			}
		}
	
	//DELETE
		@Security.Authenticated(SecuredWriter.class)
	public static Result deleteDitta(Long id) {
		
		Ditta.delete(id);
		return ok(ditta.render(Ditta.all(), dittaForm,true));
	}
	
	
	//EDIT
		@Security.Authenticated(SecuredWriter.class)
	public static Result editDitta(Long id) {
		
		Ditta daModificare=Ditta.find.byId(id);
		    
	    Form<Ditta> filledForm = Form.form(Ditta.class).bindFromRequest();
	    
		if(filledForm.hasErrors()) {
			if(Ditta.update(daModificare)){
				return badRequest(edit_ditta.render(Ditta.all(), filledForm.fill(Ditta.find.byId(daModificare.id)),id,daModificare,false));
			}
			else return redirect(routes.Ditte.ditte());
		}
	   	else{
	   		daModificare.id=id;
	   		daModificare.nome=filledForm.get().nome;
	   		daModificare.tipo=filledForm.get().tipo;
	   		daModificare.indirizzo=filledForm.get().indirizzo;
	   		daModificare.citta=filledForm.get().citta;
	   		daModificare.telefono=filledForm.get().telefono;
	   		daModificare.fax=filledForm.get().fax;
	   		daModificare.partitaIva=filledForm.get().partitaIva;
	   		daModificare.codFiscale=filledForm.get().codFiscale;
	   		daModificare.mail=filledForm.get().mail;
	   		
	   		
	   			
	   		if(!Ditta.update(daModificare)){
	   			return ok(edit_ditta.render(Ditta.all(), filledForm.fill(Ditta.find.byId(daModificare.id)),id,daModificare,true));
	   		}
	    		
	   		return ok(ditta.render(Ditta.all(), dittaForm,true)); 
		} 
	}

	//FIND
	@Security.Authenticated(Secured.class)
	public static Result findDitta() {
		Form<IdLong> idlongForm = Form.form(IdLong.class).bindFromRequest();
		
		try{
			
			Long id=Ditta.find.orderBy("nome").findList().get(0).id;
			
			if(idlongForm.get().id!=null){
				id=idlongForm.get().id;
			}
						
			if(idlongForm.hasErrors()) {
				return badRequest(find_ditta.render(Ditta.all(),idlongForm,Ditta.find.byId(id),Categoria_Ditta.all()));
			}
		   	else{
		   		return ok(find_ditta.render(Ditta.all(), idlongForm,Ditta.find.byId(id),Categoria_Ditta.all())); 
			}
		}
		catch(Exception e){
				//JOptionPane.showMessageDialog(null, "ERRORE!");
				return ok(home.render());
		}
	}
	
	//FIND BY CATEGORIA
	@Security.Authenticated(Secured.class)
	public static Result findDittaByCategoria() {
		
		
		Form<IdLong> idlongForm = Form.form(IdLong.class).bindFromRequest();
		
		try{
		
			Long id=Categoria.all().get(0).id;
			id=Categoria.find.orderBy("denominazione").findList().get(0).id;
			
			if(idlongForm.get().id!=null){
				id=idlongForm.get().id;
			}
					
			if(idlongForm.hasErrors()) {
				return badRequest(find_dittaByCategoria.render(Categoria_Ditta.all(),idlongForm,Categoria.find.byId(id),Categoria.all()));
			}
		   	else{
		   		return ok(find_dittaByCategoria.render(Categoria_Ditta.all(), idlongForm,Categoria.find.byId(id),Categoria.all())); 
			} 
		
		}
		catch(Exception e){
			//JOptionPane.showMessageDialog(null, "ERRORE!");
			return ok(home.render());
		}
	}
	
	//AGGIUNGI CATEGORIA/CLASSIFICA A DITTA
	@Security.Authenticated(SecuredWriter.class)
	public static Result aggiungiCategoria(Long id) {
		Categoria_Ditta tmp=new Categoria_Ditta();
		tmp.ditta=Ditta.find.byId(id);
		
		Form<Categoria_Ditta> filledForm = Form.form(Categoria_Ditta.class).bindFromRequest();
		
		return ok(nuova_categoria_ditta.render(filledForm.fill(tmp),Categoria.all(),Classifica.all()));
	}
}
